package com.github.niefy.modules.wx.manage;

import com.github.niefy.common.utils.PageUtils;
import com.github.niefy.common.utils.R;
import com.github.niefy.modules.sys.entity.SysUserEntity;
import com.github.niefy.modules.wx.entity.AnimalNews;
import com.github.niefy.modules.wx.service.AnimalNewsService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;

/**
 * @Description:
 * @Company：乙盟信息
 * @Author: jiangheng
 * @Date: 2022/1/18 17:01
 * @Version 1.0
 */
@RestController
@RequestMapping("/animal/article")
public class AnimalNewsController {
    @Autowired
    private AnimalNewsService animalNewsService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = animalNewsService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody AnimalNews animalNews) {
        //获取当前用户
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
       animalNews.setUserName(sysUserEntity.getUsername());
        LocalDateTime localDate = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String dataTime = localDate.format(formatter);
        animalNews.setCreateTime(dataTime);
        animalNewsService.save(animalNews);
        return R.ok();
    }

    /**
     * @return
     * @Date 2022/1/23 17:32
     * @Author jiangheng
     * @Description //TODO 根据id 查询
     **/

    @GetMapping("/info/byId/{id}")
    public R selectById(@PathVariable("id") String id){
       AnimalNews animalNews = animalNewsService.getById(id);
       return R.ok().put("article", animalNews);
    }

    /**
     * @return
     * @Date 2022/1/23 17:52
     * @Author jiangheng
     * @Description //TODO 删除
     **/

    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids) {
        animalNewsService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
